from django.contrib.auth.models import Permission, Group
from django.contrib.contenttypes.models import ContentType
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from users.models import User

# GET /meiduo_admin/permission/perms/
from meiduo_admin.serializers.permissionss import PermissionSerializer, ContentTypeSerializer, GroupSerializer, \
    PermissionSimpleSerializer, AdminSerializer


class PermissionViewSet(ModelViewSet):
    permission_classes = [IsAdminUser]
    #指定视图所使用的查询集
    queryset = Permission.objects.all()
    #指定视图所使用的序列化器类
    serializer_class = PermissionSerializer



    #获取权限类型列表数据
    # GET `/meiduo_admin/permission/content_types/`
    def content_types(self, request):
        """
        获取权限类型：
        1. 获取权限类型数据
        2. 将权限类型数据序列化并返回
        """
        # 1. 获取权限类型数据
        content_types = ContentType.objects.all()

        # 2. 将权限类型数据序列化并返回
        serializer = ContentTypeSerializer(content_types, many=True)
        return Response(serializer.data)

# GET  `/meiduo_admin/permission/groups/`


#用户组管理
class GroupViewSet(ModelViewSet):
        permission_classes = [IsAdminUser]

        # 指定视图所使用的查询集
        queryset = Group.objects.all()

        # 指定视图所使用的序列化器类
        serializer_class = GroupSerializer

        # GET `/meiduo_admin/permission/simple/`
        def simple(self, request):
            """
            获取权限数据:
            1. 获取权限数据
            2. 将权限数据序列化并返回
            """
            # 1. 获取权限数据
            permissions = Permission.objects.all()

            # 2. 将权限数据序列化并返回
            serializer = PermissionSimpleSerializer(permissions, many=True)
            return Response(serializer.data)

#管理员管理
#获取管理员用户列表数据
#GET /meiduo_admin/permission/admins/
class AdminViewSet(ModelViewSet):
    permission_classes = [IsAdminUser]

    queryset = User.objects.filter(is_staff=True)
    serializer_class = AdminSerializer

    #GET /meiduo_admin/permission/groups/simple/
    def simple(self,request):
        #获取用户组数据
        #1.获取用户组数据
        group=Group.objects.all()
        #2.将用户组数据序列化并返回
        serializer=GroupSerializer(group,many=True)
        return Response(serializer.data)

    #POST /meiduo_admin/permission/admins/



